<template>
<div style="flex:1;" class="flex auto">
  <criteria-paged :model="model" v-ref:paged  @sort="sort">
		<criteria partial='criteria' v-ref:criteria @condition-changed="$parent.selfSearch">
	  	<div novalidate class="form-inline" partial>

        <div class="form-group">
				  <input type="text" class="form-control" v-model="model.f_userinfo_id"
					condition="f_userinfo_id='{}'" placeholder='用户编号'
          :size="model.f_userinfo_id ? model.f_userinfo_id.length : 4">
        </div>
        <!-- <div class="form-group">
          <input  type="text" class="form-control" v-model="model.f_user_phone" placeholder='电话'
          condition="f_user_phone = '{}'"
          :size="model.f_user_phone ? model.f_user_phone.length : 2">
        </div> -->
				<div class="form-group">
				  <input type="text" class="form-control" v-model="model.f_user_id"
					condition="f_user_id='{}'" placeholder='表编号'
          :size="model.f_user_id ? model.f_user_id.length : 3">
        </div>
        <div class="form-group">
          <input  type="text" class="form-control" v-model="model.f_user_name" placeholder='用户姓名'
          condition="f_user_name like '%{}%'"
          :size="model.f_user_name ? model.f_user_name.length * 2 : 4">
        </div>
        <div class="form-group">
          <input type="text" class="form-control" v-model="model.f_area"
          condition="f_area like '%{}%'" placeholder='区域'
          :size="model.f_area ? model.f_area.length : 2">
        </div>
        <div class="form-group">
          <input type="text" class="form-control" v-model="model.f_residential_area"
          condition="f_residential_area like '%{}%'" placeholder='小区'
          :size="model.f_residential_area ? model.f_residential_area.length * 2 : 2">
        </div>
        <div class="form-group">
          <input type="text" class="form-control" v-model="model.f_address"
					condition="f_address like '%{}%'" placeholder='用户地址'
          :size="model.f_address ? model.f_address.length * 2: 4">
        </div>
        <div class="form-group">
          <v-select :value.sync="model.f_inputtor" v-model='model.f_inputtor'
              :options='$parent.$parent.inputtores' placeholder='抄表员'
              close-on-select
              condition="f_inputtor = '{}'" v-el:cc></v-select>
        </div>
        <div class="form-group">
          <v-select :value.sync="model.f_whether_pay" v-model='model.f_whether_pay'
              :options='$parent.$parent.whetherpaies' placeholder='是否缴费'
              close-on-select
              condition="f_whether_pay = '{}'"></v-select>
        </div>
        <div class="form-group">
          <v-select :value.sync="model.f_meter_state" v-model='model.f_meter_state'
              :options='$parent.$parent.meterstates' placeholder='抄表状态'
              close-on-select
              condition="f_meter_state = '{}'"></v-select>
        </div>
        <div class="form-group">
          <v-select :value.sync="model.f_fire_state" v-model="model.f_fire_state"
              :options='$parent.$parent.firestates' placeholder='点火状态'
              close-on-select condition="f_fire_state='{}'"></v-select>
        </div>
        <!-- <div class="form-group">
          <v-select :value.sync="model.f_operator" v-model='model.f_operator'
              :options='$parent.$parent.operatores' placeholder='操作员'
              close-on-select
              condition="f_operator = '{}'"></v-select>
        </div> -->
        <!-- <div class="form-group">
          <v-select :value.sync="model.f_outlets" v-model='model.f_outlets'
              :options='$parent.$parent.outlets' placeholder='营业网点'
              close-on-select
              condition="f_outlets = '{}'"></v-select>
        </div> -->
        <!-- <div class="form-group">
          <v-select id="f_meter_brand"
            v-model="model.f_meter_brand"
            placeholder='气表品牌'
            :value.sync="model.f_meter_brand"
            :options='$parent.$parent.brand'
            close-on-select >
          </v-select>
        </div> -->
        <div class="form-group" v-if="$parent.$parent.isHead()">
          <v-select :value.sync="model.f_filiale" v-model='model.f_filiale'
              :options='$parent.$parent.filiales' placeholder='分公司'
              close-on-select
              condition="f_filiale = '{}'"></v-select>
        </div>
        <div class="form-group">
          <v-select :value.sync="model.f_meter_classify" v-model="model.f_meter_classify"
              :options='$parent.$parent.meters' placeholder='气表类型'
              close-on-select
              condition="f_meter_classify = '{}'"
              default-value="'机表'"></v-select>
        </div>
        <div class="form-group">
          <v-select :value.sync="model.meternumber_state" v-model="model.meternumber_state"
              :options='$parent.$parent.meternumbers' placeholder='是否有表号'
              close-on-select
              condition="meternumber_state = '{}'"></v-select>
        </div>
        <div class="form-group">
          <v-select :value.sync="model.f_meter_source" v-model="model.f_meter_source"
              :options='$parent.$parent.sources' placeholder='数据来源'
              close-on-select
              condition="f_meter_source = '{}'"></v-select>
        </div>
        <div class="form-group">
          <v-select :value.sync="model.f_issued_state" v-model="model.f_issued_state"
              :options='$parent.$parent.issueds' placeholder='下发状态'
              close-on-select
              condition="f_issued_state = '{}'"></v-select>
        </div>
        <div class="form-group">
          <button class="btn btn-success  width-100" @click="search()">查询</button>
        </div>

        <div class="form-group" v-if="$parent.$parent.meter_state">
          <button class="btn btn-success  width-100" @click="$parent.$parent.batchDownHand()" >计划下发</button>
        </div>
        <div class="form-group">
          <button class="btn btn-success  width-100" @click="$parent.$parent.adjust()">抄表员调整</button>
        </div>
        <div class="form-group">
          <button class="btn btn-success width-100" @click="$parent.$parent.fileUpload()">文件上传</button>
          <!-- <file-upload class="my-file-uploader" name="myFile"
          action="rs/file/upload" tagname="文件上传" :headers="$parent.$parent.headers"></file-upload> -->
        </div>
        <div class="form-group">
          <button class="btn btn-success  width-60" @click="search(), $parent.$parent.clean()">打印</button>
        </div>
        <div class="form-group">
          <export-excel :data="{startDate: $parent.$parent.startDate, endDate: $parent.$parent.endDate, condition: $parent.$parent.model.condition}"
           bean='抄表导出' sqlurl="rs/logic/exportfile"></export-excel>
        </div>
				<!-- <button class="btn btn-success" @click="$parent.$parent.confirmHand()">保存录入</button> -->
				<!-- <export-excel :data="{orderitem: 'id', condition: '1=1'}" sqlurl="rs/excel/report/handManager/抄表导出Excel模板"></export-excel> -->
				<!-- <form-print :model='$parent.model' url='rs/report/test' top='88' left='200' width='350' height='600'></form-print> -->
        <!-- <print-table2 id='test' top='60' left='20' width='100%' height='100%'></print-table2> -->
	  	</div>
	 	</criteria>
		<data-grid :model="model" partial='list' v-ref:grid id="test" >
	  	<template partial='head'>
			<tr>
				<th>序号</th>
				<th>
             <data-order field="f_userinfo_id" name="用户编号" :order.sync="$parent.$parent.$parent.orderFields.f_userinfo_id"></data-order>
        </th>
        <th>表编号</th>
				<th>用户基本信息</th>
        <th>抄表员</th>
				<!-- <th>表具信息</th> -->
				<th>上次抄表底数</th>
				<th>本次抄表底数</th>
				<th>应交气量</th>
        <th>是否缴费</th>
        <th>数据来源</th>
        <th>下发状态</th>
        <th>操作</th>
				<!-- <th>操作</th> -->
			</tr>
		  </template>
		  <template partial='body' >
				<td style="text-align: center;">{{$index+1}}</td>
        <td>{{row.f_userinfo_id}}</td>
        <td>{{row.f_user_id}}</td>
        <td>{{row.f_user_name}}&nbsp;&nbsp;{{row.f_phone}}&nbsp;&nbsp;{{row.f_address}}</td>
        <td>{{row.f_inputtor}}</td>
        <!-- <td>{{row.f_gasproperties}}-{{row.f_gasmeterstyle}}-{{row.f_gaswatchbrand}}
          -{{row.f_metertype}}-{{row.f_gasmetermanufacturers}}-{{row.f_aliasname}}</td>  -->
				<td>{{row.f_meter_base}}</td>
				<td id="hand-creat">
					<div class="form-group" style="margin-bottom:0px;" :class="{
						'has-success has-feedback': (row.f_tablebase && row.f_tablebase !== '')? ((row.f_tablebase-0) >= (row.f_meter_base -0)) : false,
						'has-error has-feedback': (row.f_tablebase && row.f_tablebase !== '')? ((row.f_tablebase-0) < (row.f_meter_base -0) ) : false}"
						v-if="row.f_meter_state === '未抄表' && row.states !== 0">

					  <input type="text" class="form-control" v-model="row.f_tablebase"  @blur="$parent.$parent.$parent.beforenter($index, row)"
            v-next-el="{id: $index+1}"   :id="$index" style="width: 100px;padding: 6px;"  @focus="$parent.$parent.$parent.clearError(row)"
            :disabled="row.state === '开始'">
					  <span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"
						v-show="(row.f_tablebase && row.f_tablebase !== '')? ((row.f_tablebase-0) >= (row.f_meter_base -0)) : false"></span>
            <span class="hand-loading" v-if="row.state === '开始'"></span>
					  <!-- <span id="inputSuccess2Status" class="sr-only">(success)</span> -->
						<span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true"
						v-show="(row.f_tablebase && row.f_tablebase !== '')? ((row.f_tablebase-0) < (row.f_meter_base -0) ) : false"></span>
						<!-- <span id="inputError2Status" class="sr-only">(error)</span> -->
					</div>
          <div id="handManager" v-else>
            <span v-if="row.states === 0">{{row.f_tablebase ? row.f_tablebase : '本期无计划'}}</span>
            <span v-if="row.states === 1">{{row.f_tablebase ? row.f_tablebase : '尚未下发'}}</span>
          </div>
          <span v-if="row.error" style="color: red;">{{row.error}}</span>
				</td>
				<td>{{row.f_oughtamount =row.f_tablebase ? (row.f_tablebase-0 - (row.f_meter_base -0)).toFixed(2) : 0}}</td>
        <td style="text-align: center;">{{row.f_whether_pay}}</td>
        <td style="text-align: center;">{{row.f_meter_source}}</td>
        <td style="text-align: center;">{{row.f_issued_state}}</td>
        <td style="text-align: center;">
          <button type="button" name="button" class="btn btn-link"
          v-if="row.f_meter_state === '已抄表' && row.f_whether_pay === '否' && row.states === 1"
          @click.stop="$parent.$parent.$parent.cancelHand(row)">冲正</button>

          <button type="button" name="button" class="btn btn-link"
          @click.stop="$parent.$parent.$parent.delectHand(row)  && row.states === 1"
          v-if="row.f_meter_state === '未抄表' && row.states === 1">删除</button>

          <button type="button" name="button" class="btn btn-link"
          @click.stop="$parent.$parent.$parent.downHand(row)"
          v-if="row.f_meter_state === '未下发'">下发计划</button>
          <!-- <button type="button" name="button" class="btn btn-link"
          @click.stop="$parent.$parent.$parent.downHand(row)"
          v-if="row.f_meter_state === '未下发' && row.states === 1">下发计划</button> -->

          <!-- <button type="button" name="button" class="btn btn-link"
          @click.stop="$parent.$parent.$parent.handReverse(row) && row.states === 1"
          v-if="row.f_whether_pay === '是'">冲正</button> -->
        </td>
		  </template>
	  </data-grid>
   </criteria-paged>
   <div style="height: 200px;" class="form-inline" v-if="isChange">
     <div class="form-group auto">
       <label>调整用户数</label>
       <input class="form-control" v-model="model.count" disabled>
     </div>
      <div class="form-group auto">
        <label>抄表员</label>
        <v-select :value.sync="changeModel.f_inputtor" v-model='changeModel.f_inputtor'
          :options='inputtores' placeholder='抄表员'
          close-on-select
          condition="h.f_inputtor = '{}'" v-el:cc></v-select>
      </div>
      <button class="btn btn-success" @click="changeInputtor(changeModel.f_inputtor[0])" :disabled="changeModel.f_inputtor.length !== 1">确认</button>
      <button class="btn btn-success" @click="changeCancel()">取消</button>
   </div>

   <modal :show.sync="show" v-ref:modal backdrop="false">
     <header slot="modal-header" class="modal-header">
       <h4 class="modal-title">计划下发</h4>
     </header>
     <article slot="modal-body" class="modal-body">
       <div class="form-group">
         {{msg}}
       </div>
       <div class="form-group">
         <label class="label-control">下发日期</label>
         <!-- <input  type="text" class="form-control" v-model="downDate" placeholder='下发日期'> -->
         <datepicker  placeholder="下发日期" :value.sync="downDate"></datepicker>
       </div>
       <!-- <div class="form-group" style="color: red;" v-if="dateError">
         日期格式错误
       </div> -->
       <!-- <div class="form-group">
         为保证数据统一，下发日期格式统一为yyyyMM，例如："201709"
       </div> -->
     </article>

     <footer slot="modal-footer" class="modal-footer">
       <button v-show="show" type="button" class="btn btn-default" @click='close'>取消</button>
       <button v-show="show" type="button" class="btn btn-success" @click='confirm()'>确认</button>
     </footer>
   </modal>
   <modal :show.sync="uploadshow" v-ref:modal backdrop="false">
     <header slot="modal-header" class="modal-header">
       <button type="button" class="close" @click="close"><span>&times;</span></button>
       <h4 class="modal-title">文件上传</h4>
     </header>
     <article slot="modal-body" class="modal-body">
       <div class="form-group">
         <file-upload class="my-file-uploader" name="myFile"
         action="rs/file/upload" tagname="文件上传" :headers="headers" multiple v-ref:file></file-upload>
       </div>
     </article>

     <footer slot="modal-footer" class="modal-footer">
       <!-- <button v-show="show" type="button" class="btn btn-default" @click='close'>取消</button>

       <button v-show="show" type="button" class="btn btn-success" @click='confirm(filename)'>确认</button> -->
     </footer>
   </modal>
 </div>
</template>
<script>
import { PagedList, HttpResetClass } from 'vue-client'
import Vue from 'vue'
import AppData from '../../../stores/AppData'
import * as Util from '../../Util'

export default {
  title: '抄表管理',
  data () {
    return {
      model: new PagedList('rs/sql/periodAlreadyIssued', 50, {orderitem: '"id"', startDate: 'this.model.startDate',
             endDate: 'this.model.endDate'}),
      changeModel: {
        f_inputtor: ''
      },
      isChange: false,
      meter_state: false, // 是否显示计划下发按钮
      show: false,
      uploadshow: false,
      downDate: Util.toStandardDateString(),
     //  downDate: Util.toStandardYearMonth().replace("-", ""),
      msg: '',
      condition: '',
      headers: {'username': Util.f.name, 'blodid': Util.guid()},
      meters: [
        {label: '全部', value: ''},
        {label: '机表', value: '机表'},
        {label: '远传表', value: '远传表'}
      ],
      meternumbers: [
        {label: '全部', value: ''},
        {label: '有表号', value: '有表号'},
        {label: '无表号', value: '无表号'}
      ],
      sources: [
        {label: '全部', value: ''},
        {label: '手动录入', value: '手动录入'},
        {label: '抄表导入', value: '抄表导入'}
      ],
      issueds: [
        {label: '全部', value: ''},
        {label: '手动下发', value: '手动下发'},
        {label: '未抄下发', value: '未抄下发'},
        {label: '失败下发', value: '失败下发'},
        {label: '异常下发', value: '异常下发'},
        {label: '底数异常下发', value: '底数异常下发'}
      ],
     //  f_meter_classify: ['机表'],
      orderDefault: 'id',
      orderFields: {
        f_userinfo_id: 'no'
      }
    }
  },
  props: {
    operater: {
      type: String
    },
    sgnetwork: {
      type: String
    },
    startDate: {
      type: String
    },
    endDate: {
      type: String
    }
  },
  ready () {
    this.model.startDate = this.startDate
    this.model.endDate = this.endDate
    // if (this.f_meter_classify[0] !== '') {
    //   this.model.f_meter_classify = `u.f_meter_classify = '${this.f_meter_classify[0]}' `
    // }else {
    //   this.model.f_meter_classify = '1=1'
    // }
    // this.search({condition: '1=1'})
    this.reflash()
  },
  methods: {
    search (datas) {
      this.changeCancel()
      this.meter_state = datas.condition.includes('未下发')
      if (!Util.f.parent.parent.name.includes('统一资源管理')) {
        datas.condition += ` and f_filiale = '${Util.f.f_fengongsi}'`
      }
      if (this.$refs.paged.$refs.criteria.model.f_meter_type) {
        datas.condition += ` and f_filiale = '${Util.f.f_fengongsi}'`
      }
      this.model.search(datas.condition, this.model)
    },
    selfSearch (datas) {
      this.changeCancel()
      this.meter_state = datas.condition.includes('未下发')
      if (!Util.f.parent.parent.name.includes('统一资源管理')) {
        datas.condition += ` and f_filiale = '${Util.f.f_fengongsi}'`
      }
      // if (this.$refs.paged.$refs.criteria.model.f_meter_type.length === 1 && this.$refs.paged.$refs.criteria.model.f_meter_type[0] !== '') {
      //   datas.condition += `and f_meter_type = '${this.$refs.paged.$refs.criteria.model.f_meter_type[0]}'`
      // }
      if (datas.model.f_meter_brand && datas.model.f_meter_brand.length > 0) {
        if (datas.model.f_meter_brand[0].f_meter_brand) {
          datas.condition = `${datas.condition} and f_meter_brand = '${datas.model.f_meter_brand[0].f_meter_brand}'`
        }
      }
      this.model.search(datas.condition, this.model)
    },
    searchGather (inputtor, state, num) {
      this.$refs.paged.$refs.criteria.model.f_inputtor = [inputtor]
      this.$refs.paged.$refs.criteria.model.f_meter_state = [state]
      this.$refs.paged.$refs.criteria.model.f_user_id = ''
      this.$refs.paged.$refs.criteria.model.f_userinfo_id = ''
      this.$refs.paged.$refs.criteria.model.f_address = ''
      this.reflash()
    },
    reflash () {
      this.$refs.paged.$refs.criteria.search()
    },
    beforenter (index, row) {
      let baojingas = AppData.getSingleValue('报警气量')
      let jujdegas = row.f_tablebase - row.f_meter_base - baojingas
      if (jujdegas >= 0) {
        this.$showMessage('您输入的表底数和上次表底数的差值大于或等于报警气量，您确定要选择输入此值吗？', ['cancel', 'confirm'])
        .then((res) => {
          if (res === 'confirm') {
            this.enter(index, row)
          } else if (res === 'cancel') {
            row.f_tablebase = 0
            return
          }
        })
      } else {
        this.enter(index, row)
      }
    },
    enter (index, row) {
      row.f_operator = Util.f.name
      row.f_inputtor = row.f_inputtor
      // row.f_end_date = Util.addDate(Util.toStandardTimeString(), AppData.getSingleValue('滞纳期限'))
      row.f_userinfo_id = row.f_userinfo_id + ''
      row.f_user_id = row.f_user_id + ''
      row.f_filiale = Util.f.f_fengongsi
      row.f_outlets = Util.f.f_parentname
      row.userfile = {
        f_user_id: row.f_user_id,
        f_meter_base: row.f_tablebase,
        version: row.version
      }
      if (row.f_remanent_gas >= row.f_oughtamount) {
        row.userfile.f_remanent_gas = (row.f_remanent_gas - 0) - (row.f_oughtamount - 0)
      } else {
        row.userfile.f_remanent_gas = 0
      }
      if (row.f_tablebase && row.f_tablebase !== '' && row.f_oughtamount >= 0) {
        Vue.set(row, 'state', '开始')
        row.http = new HttpResetClass()
        row.http.load('POST', 'rs/logic/handEnter', {data: row}, {resolveMsg: null, rejectMsg: null}).then((data) => {
          row.f_meter_state = data.data.f_meter_state
          row.f_whether_pay = data.data.f_whether_pay
          row.f_sell_id = data.data.f_sell_id
          row.version = data.data.version
          row.state = '成功'
          this.$parent.search()
        }).catch((error) => {
          row.state = '失败'
          Vue.set(row, 'error', JSON.stringify(error))
        })
      }
    },
    clearError (row) {
      if (row.f_tablebase === 0) {
        row.f_tablebase = ''
      }
      Vue.set(row, 'error', null)
    },
    // 抄表员调整
    adjust () {
      // 首先查看是否有已录入的用户，如果有，就弹出提示框，提示重新输入查询条件，或撤销已录入
      this.isChange = true
    },
    fileUpload () {
      this.uploadshow = true
    },
    changeCancel () {
      this.isChange = false
      this.changeModel.f_inputtor = ''
    },
    // 删除这条计划
    delectHand (row) {
      this.$showMessage('删除后不可恢复，确认删除吗？', ['confirm', 'cancel']).then((res) => {
        if (res === 'confirm') {
          this.$resetpost('rs/logic/deletePlan', {data: row}).then(() => {
            this.$dispatch('delect-hand', '删除计划', row)
          })
        }
      })
    },
    // 撤销录入
    cancelHand (row) {
      this.$showMessage('撤销后不可恢复，需要重新录入，确认撤销吗？', ['confirm', 'cancel']).then((res) => {
        if (res === 'confirm') {
          // 还原t_userfiles中的f_meter_base
          // row.userfile = {
          //   f_user_id: row.f_user_id,
          //   f_meter_base: row.f_meter_base,
          //   version: row.version
          // }
          // row.f_tablebase = 0
          // row.f_meter_state = '未抄表'
          // row.f_whether_pay = '否'
          // 组织冲正数据
          let data = {
            id: row.id,
            f_haircut_gas: row.f_haircut_gas,
            f_remanent_gas: row.f_remanent_gas,
            f_user_id: row.f_user_id,
            f_meter_base: row.f_meter_base
          }
          data.newcorrent = {
            f_user_id: row.f_user_id,
            f_userinfo_id: row.f_userinfo_id,
            f_correct_type: '抄表冲正',
            f_correct_id: row.id,
            f_base: row.f_meter_base,
            f_last_base: row.f_tablebase,
            f_correct_money: 0 - row.f_oughtfee,
            f_correct_date: Util.toStandardTimeString(),
            f_correct_state: '有效',
            f_outlets: Util.f.f_parentname,
            f_filiale: Util.f.f_fengongsi,
            f_operator: Util.f.name
          }
          data.delplan = {
            f_userinfo_id: row.f_userinfo_id,
            f_meter_state: '未抄表',
            f_hand_date: row.f_hand_date,
            f_this_data: Util.toStandardTimeString()
          }
          data.newplan = {
            f_last_tablebase: row.f_meter_base,
            f_inputtor: row.f_inputtor,
            f_whether_pay: '否',
            f_meter_state: '未抄表',
            f_price_id: row.f_price_id,
            userfile: row.f_user_id,
            f_userinfo_id: row.f_userinfo_id + '',
            f_user_name: row.f_user_name,
            f_user_type: row.f_user_type,
            f_gasproperties: row.f_gasproperties,
            f_address: row.f_address,
            f_meter_brand: row.f_meter_brand,
            f_meter_style: row.f_meter_style,
            f_meternumber: row.f_meternumber,
            f_filiale: Util.f.f_fengongsi,
            f_outlets: Util.f.f_parentname,
            f_operator: Util.f.name
          }
          // data.correctdata = {
          //   version: row.version,
          //   f_user_id: row.f_user_id + '',
          //   f_userinfo_id: row.f_userinfo_id + '',
          //   f_correct_type: '抄表冲正',
          //   f_correct_id: row.id,
          //   f_base: row.f_meter_base,
          //   f_last_base: row.f_tablebase,
          //   f_correct_money: 0 - row.f_oughtfee,
          //   f_correct_state: '有效',
          //   f_filiale: Util.f.f_fengongsi,
          //   f_outlets: Util.f.f_parentname,
          //   f_operator: Util.f.name
          // }
          // data.newHandplan = {
          //   f_hand_date: row.f_hand_date,
          //   f_last_tablebase: row.f_meter_base,
          //   f_inputtor: row.f_inputtor,
          //   f_whether_pay: '否',
          //   f_meter_state: '未抄表',
          //   f_price_id: row.f_price_id,
          //   userfile: row.f_user_id,
          //   f_userinfo_id: row.f_userinfo_id + '',
          //   f_user_name: row.f_user_name,
          //   f_user_type: row.f_user_type,
          //   f_gasproperties: row.f_gasproperties,
          //   f_address: row.f_address,
          //   f_meter_brand: row.f_meter_brand,
          //   f_meter_style: row.f_meter_style,
          //   f_meternumber: row.f_meternumber,
          //   f_filiale: Util.f.f_fengongsi,
          //   f_outlets: Util.f.f_parentname,
          //   f_operator: Util.f.name
          // }
          // 还原t_handplan
          this.$resetpost('rs/logic/cancelReverse', {data: data}).then(() => {
            this.$dispatch('cancel-hand', '撤销录入')
          })
        }
      })
    },
    handReverse (row) {
      this.$showMessage('冲正后需要重新缴费，确认冲正吗？', ['confirm', 'cancel']).then((res) => {
        if (res === 'confirm') {
          // 还原t_userfiles中的f_meter_base
          let data = {
            sell_id: row.f_sell_id,
            handplan_id: row.id,
            userinfo_version: row.userinfo_version
          }
          data.cancel = {
            f_user_id: row.f_user_id,
            f_user_name: row.f_user_name,
            f_correct_type: '缴费冲正',
            f_correct_id: row.id,
            f_correct_cause: '机表缴费错误',
            f_correct_money: 0 - row.f_oughtfee,
            f_correct_state: '有效',
            f_correct_gas: 0 - row.f_oughtamount,
            f_operator: Util.f.name
          }
          // 还原t_handplan
          this.$resetpost('rs/logic/handReverse', {data: data}).then(() => {
            this.$dispatch('hand-reverse', '冲正')
          })
        }
      })
    },
    // 批量变更抄表员
    changeInputtor (inputtor) {
      if (this.model.condition.includes('f_meter_brand')) {
        this.model.condition = this.model.condition.replace('f_meter_brand', 'g.f_meter_brand')
      }
      this.$showMessage(`确认变更前请确认选择的事件区间和查询条件是否正确，确认变更当前 ${this.model.count} 户抄表员为 ${inputtor}`, ['confirm', 'cancel']).then((res) => {
        if (res === 'confirm') {
          let http = new HttpResetClass()
          http.load('POST', 'rs/logic/changeInputtor',
          {data: {startDate: this.startDate, endDate: this.endDate, condition: this.model.condition, inputtor: inputtor}}, {resolveMsg: null}).then((data) => {
            this.$showAlert(`总计变更 ${data.data} 户抄表员为 ${inputtor}`, 'success', 2000)
            this.model.loadPage(this.model.pageIndex)
            this.changeCancel()
            this.$dispatch('change-inputtor', '抄表员批量变更', this.model.count, inputtor)
          })
        }
      })
    },
    down (type, condition, data) {
      this.condition = condition
      this.msg = '确认下发计划'
      if (type === 'single') {
        this.msg = `确认下发编号为： ${data.f_userinfo_id} 户计划`
      } else if (type === 'batch') {
        this.msg = `确认下发当前总计:  ${data}  户计划`
      }
      // this.downDate = Util.toStandardDateString()
      this.show = true
      // let str = '确认下发计划'
      // if (type === 'single') {
      //   str = `确认下发编号为： ${data.f_userinfo_id} 户计划`
      // } else if (type === 'batch') {
      //   str = `确认下发当前总计:  ${data}  户计划`
      // }
      // this.$showMessage(str, ['confirm', 'cancel']).then((res) => {
      //   if (res === 'confirm') {
      //     if (!Util.f.parent.parent.name.includes('统一资源管理')) {
      //       condition += `f_filiale = '${Util.f.f_fengongsi}'`
      //     }
      //     this.$resetpost('rs/logic/downHand', {data: {startDate: this.startDate, endDate: this.endDate, condition: condition, f_operator: Util.f.name, downDate: this.downDate}}
      //     , {resolveMsg: null, rejectMsg: null}).then((res) => {
      //       // this.search({condition: this.condition || '1=1'})
      //       this.$dispatch('down-complete', '下发计划完成', res.data)
      //       this.$showAlert(`全部下发,总计下发${res.data}户`, 'success', 2000)
      //     }).catch((error) => {
      //       this.$showAlert(`下发失败,请注意查看，失败原因：${error}`, 'danger', 0)
      //     })
      //   }
      // })
    },
    // 单条下发计划
    downHand (row) {
      this.down('single', `f_user_id = '${row.f_user_id}'`, row)
    },
    // 批量下发计划
    batchDownHand () {
      this.down('batch', this.model.condition, this.model.count)
    },
    confirm () {
      // 正式提交下发
      this.show = false
      // this.dateError = false
      // if (!Util.f.parent.parent.name.includes('统一资源管理')) {
      //   condition += `f_filiale = '${Util.f.f_fengongsi}'`
      // }
      // 远传表不能下发
      // let meter = `u.f_meter_type = '机表'`
      // if (this.$refs.paged.$refs.criteria.model.f_meter_type[0] !== '') {
      //   meter = `u.f_meter_type = '${this.$refs.paged.$refs.criteria.model.f_meter_type[0]}' `
      // }
      this.$resetpost('rs/logic/downHand', {data: {downDate: this.downDate, startDate: this.startDate,
        endDate: this.endDate, condition: this.condition, f_operator: Util.f.name }}
      , {resolveMsg: null, rejectMsg: null}).then((res) => {
        // this.search({condition: this.condition || '1=1'})
        this.$dispatch('down-complete', '下发计划完成', res.data)
        this.$showAlert(`全部下发,总计下发${res.data}户`, 'success', 2000)
      }).catch((error) => {
        this.$showAlert(`下发失败,请注意查看，失败原因：${error}`, 'danger', 0)
      })
    },
    // 关闭下发计划得弹出框
    close () {
      this.show = false
      if (this.uploadshow) {
        this.uploadshow = false
        // 讲选的文件清空
        this.$refs.file.$el.querySelector('input').value = ''
      }
    },
    // 判断当前登陆人是不是子公司的人员
    isHead () {
      return Util.f.parent.parent.name.includes('统一资源管理')
    },
    sort (field, rule) {
      // 将所有排序方式设为不排序，实现相互排斥
      for (let key in this.orderFields) {
        if (key === field) {
          this.orderFields[key] = rule
        } else {
          this.orderFields[key] = 'no'
        }
      }
      // 如果新规则不排序，还原为默认排序
      if (rule === 'no') {
        this.model.paramSource.orderitem = `'${this.orderDefault}'`
      } else {
        this.model.paramSource.orderitem = `'${field} ${rule}'`
      }

      this.reflash()
    }
  },
  computed: {
    inputtores () {
      return [{label: '全部', value: ''}, ...AppData.getParam('抄表员')]
    },
    meterstates () {
      return [{label: '全部', value: ''}, ...AppData.getParam('抄表状态')]
    },
    filiales () {
      return [{label: '全部', value: ''}, ...AppData.getParam('分公司')]
    },
    // operatores () {
    //   return [{label: '全部', value: ''}, ...AppData.getParam('操作员')]
    // },
    // outlets () {
    //   return [{label: '全部', value: ''}, ...AppData.getParam('营业网点')]
    // },
    whetherpaies () {
      // return [{label: '全部', value: ''}, ...AppData.getParam('缴费状态')]
      return [
        {label: '全部', value: ''},
        {label: '已缴费', value: '是'},
        {label: '未缴费', value: '否'}
      ]
    },
    // 校验日期输入格式
    dateError () {
      // 校验是不是纯数字
      if (!(/^[0-9]+$/.test(this.downDate))) {
        return true
      }
      // 校验是不是6位长度
      if (this.downDate.length !== 6) {
        return true
      }
      // let year = this.downDate.substring(0, 4)
      let month = this.downDate.substring(4, 6)
      // 月份是不是符合
      if ((month - 0) > 12 && (month - 0) < 1) {
        return true
      }
      return false
    },
    getStartAndEnd () {
      return Util.toStartAndEndDateString()
    },
    firestates () {
      return [
        {label: '全部', value: ''},
        {label: '已点火', value: '1'},
        {label: '未点火', value: '0'}
      ]
    },
    brand () {
      return [{label: '全部', value: {f_meter_brand: '', gasmodel: [{label: '全部', value: {f_meter_style: ''}}]}}, ...AppData.getGasbrand()]
    }
  },
  watch: {
    'startDate' (val) {
      this.model.startDate = val
    },
    'endDate' (val) {
      this.model.endDate = val
    }
    // 'f_meter_classify' (val) {
    //   if (val[0] !== '') {
    //     this.model.f_meter_classify = `u.f_meter_classify = '${val[0]}' `
    //   }else {
    //     this.model.f_meter_classify = '1=1'
    //   }
    // }
  },
  events: {
    onFileUpload: function (file, res) {
      let data = {
        // configpath: 'hibernate/用户档案/userinfo.hbm.xml',
        model: {
          f_operator: Util.f.name,
          f_end_date: Util.nextMonth20(Util.toStandardTimeString())
        },
        filepath: res.path
      }
      // console.log('2222223333333333333', data.filepath)
      // 获取filename 判断是先锋还是伦力，之后有还可在加
      // console.log('1111111111', data.filepath)
      if (data.filepath.includes('先锋')) {
        this.$resetpost('rs/logic/xfimport', data, {resolveMsg: null, rejectMsg: '上传失败'}, 0).then((res) => {
          // console.log('终于成功了', res)
          this.uploadshow = false
          this.$showAlert(`一共上传${res.data.total}条数据, 成功${res.data.successnum}条, 未抄下发${res.data.nohandnum}条, 失败下发${res.data.failednum}条, 异常下发${res.data.exceptionnum}条`, 'success', 2000)
          this.$parent.search()
        })
      } else if (data.filepath.includes('伦力')) {
        this.$resetpost('rs/logic/llimport', data, {resolveMsg: null, rejectMsg: '上传失败'}, 0).then((res) => {
          // console.log('终于成功了', res)
          this.uploadshow = false
          this.$showAlert(`一共上传${res.data.total}条数据, 成功${res.data.successnum}条, 未抄下发${res.data.nohandnum}条, 失败下发${res.data.failednum}条, 异常下发${res.data.exceptionnum}条`, 'success', 2000)
          this.$parent.search()
        })
      }
    }
  }
}
</script>
<style lang="less">
 #handManager {
   display: flex;
   justify-content: space-around;
   align-items: center;
   .btn {
     margin: 0px;
   }
 }
 .hand-loading {
   border: 3px solid #ccc;
   border-right-color: #337ab7;
   border-radius: 50%;
   animation: spin 0.6s linear;
   position: absolute;
   animation-iteration-count: infinite;
   top: 0;
   right: 50%;
   z-index: 2;
   display: block;
   width: 28px;
   height: 28px;
   line-height: 28px;
   text-align: center;
   pointer-events: none;
 }
 @keyframes spin {
   100% {
     transform: rotate(360deg);
   }
 }
</style>
